/*
 * @Author: your name
 * @Date: 2021-11-04 18:58:55
 * @LastEditTime: 2021-11-06 21:04:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-app2\src\router\index.js
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken } from '../utils/auth'
import Home from '../views/Home.vue'

const routes = [{
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/about',
        name: 'About',
        component: () =>
            import ('../views/About.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue')
    },
    {
        path: '/menu',
        name: 'Menu',
        component: () =>
            import ('../views/Menu.vue')
    },
    {
        path: '/orderconfirm',
        name: 'OrderConfirm',
        component: () =>
            import ('../views/OrderConfirm.vue')
    },
    {
        path: '/note',
        name: 'Note',
        component: () =>
            import ('../views/Note.vue')
    },
    {
        path: '/user',
        name: 'User',
        component: () =>
            import ('../views/User.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 路由前置守卫，判断是否登录
router.beforeEach((to, from, next) => {
    if (getToken()) {
        next();
    } else {
        if (to.path == "/login") {
            next()
        } else {
            next("/login")
        }
    }
})
export default router